]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
brightnessManager: Add keyboard shortcuts for monitor brightness control
authorSebastian Wick <sebastian.wick@redhat.com>
Sat, 21 Jun 2025 22:10:04 +0000 (00:10 +0200)
committerSebastian Wick <sebastian.wick@redhat.com>
Sun, 3 Aug 2025 01:34:28 +0000 (03:34 +0200)
This used to be done by g-s-d but to avoid the split brain situation,
g-s-d has to stop poking at the backlight directly. This also means that
there is no reason to round-trip through g-s-d anymore, and we can just
implement the shortcuts in gnome shell.

This also adds new shortcuts for controling the backlight of the "active
monitor" (i.e. the one with the cursor) by pressing shift as well.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3751>

data/org.gnome.shell.gschema.xml.in
js/misc/brightnessManager.js

index b1c2534f9c70b51ae7ae5635a463201f773d0d0b..9a55ee71db5d3fd12ae764f3afba2ea0931953e1 100644 (file)
       <default>["&lt;Shift&gt;Print"]</default>
       <summary>Take a screenshot</summary>
     </key>
+    <key name="screen-brightness-up" type="as">
+      <default>['XF86MonBrightnessUp']</default>
+      <summary>Screen brightness up</summary>
+    </key>
+    <key name="screen-brightness-up-monitor" type="as">
+      <default>['&lt;Shift&gt;XF86MonBrightnessUp']</default>
+      <summary>Screen brightness up on current monitor</summary>
+    </key>
+    <key name="screen-brightness-down" type="as">
+      <default>['XF86MonBrightnessDown']</default>
+      <summary>Screen brightness down</summary>
+    </key>
+    <key name="screen-brightness-down-monitor" type="as">
+      <default>['&lt;Shift&gt;XF86MonBrightnessDown']</default>
+      <summary>Screen brightness down on current monitor</summary>
+    </key>
+    <key name="screen-brightness-cycle" type="as">
+      <default>['XF86MonBrightnessCycle']</default>
+      <summary>Screen brightness cycle</summary>
+    </key>
+    <key name="screen-brightness-cycle-monitor" type="as">
+      <default>['&lt;Shift&gt;XF86MonBrightnessCycle']</default>
+      <summary>Screen brightness cycle on current monitor</summary>
+    </key>
   </schema>
 
   <schema id="org.gnome.shell.app-switcher"
index 9d6fe16e8cb0b1070348b4812f1f2e4e97799005..406c9f2dd691f4c3e813f0afcc81f4b44ab106d7 100644 (file)
@@ -1,10 +1,14 @@
 import Gio from 'gi://Gio';
 import GObject from 'gi://GObject';
+import Meta from 'gi://Meta';
+import Shell from 'gi://Shell';
 
 import * as Main from '../ui/main.js';
 
+const SCALE_VALUE_STEP = 0.1;
 const SCALE_VALUE_CHANGE_EPSILON = 0.001;
 
+const KEYBINDING_SCHEMA = 'org.gnome.shell.keybindings';
 const POWER_SCHEMA = 'org.gnome.settings-daemon.plugins.power';
 
 export const BrightnessManager = GObject.registerClass({
@@ -26,6 +30,48 @@ export const BrightnessManager = GObject.registerClass({
 
         this._abTarget = -1.0;
 
+        Main.wm.addKeybinding(
+            'screen-brightness-up',
+            new Gio.Settings({schema_id: KEYBINDING_SCHEMA}),
+            Meta.KeyBindingFlags.NONE,
+            Shell.ActionMode.ALL,
+            this._screenBrightnessUp.bind(this));
+
+        Main.wm.addKeybinding(
+            'screen-brightness-up-monitor',
+            new Gio.Settings({schema_id: KEYBINDING_SCHEMA}),
+            Meta.KeyBindingFlags.NONE,
+            Shell.ActionMode.ALL,
+            this._screenBrightnessUpCurrentMonitor.bind(this));
+
+        Main.wm.addKeybinding(
+            'screen-brightness-down',
+            new Gio.Settings({schema_id: KEYBINDING_SCHEMA}),
+            Meta.KeyBindingFlags.NONE,
+            Shell.ActionMode.ALL,
+            this._screenBrightnessDown.bind(this));
+
+        Main.wm.addKeybinding(
+            'screen-brightness-down-monitor',
+            new Gio.Settings({schema_id: KEYBINDING_SCHEMA}),
+            Meta.KeyBindingFlags.NONE,
+            Shell.ActionMode.ALL,
+            this._screenBrightnessDownCurrentMonitor.bind(this));
+
+        Main.wm.addKeybinding(
+            'screen-brightness-cycle',
+            new Gio.Settings({schema_id: KEYBINDING_SCHEMA}),
+            Meta.KeyBindingFlags.NONE,
+            Shell.ActionMode.ALL,
+            this._screenBrightnessCycle.bind(this));
+
+        Main.wm.addKeybinding(
+            'screen-brightness-cycle-monitor',
+            new Gio.Settings({schema_id: KEYBINDING_SCHEMA}),
+            Meta.KeyBindingFlags.NONE,
+            Shell.ActionMode.ALL,
+            this._screenBrightnessCycleCurrentMonitor.bind(this));
+
         const monitorManager = global.backend.get_monitor_manager();
         monitorManager.connectObject('monitors-changed',
             this._monitorsChanged.bind(this), this);
@@ -58,6 +104,33 @@ export const BrightnessManager = GObject.registerClass({
         return [...this._monitorScales.values()];
     }
 
+    _screenBrightnessUp() {
+        this._globalScale?.stepUp();
+    }
+
+    _screenBrightnessUpCurrentMonitor() {
+        const monitor = global.backend.get_current_logical_monitor();
+        this._monitorScales.get(monitor)?.stepUp();
+    }
+
+    _screenBrightnessDown() {
+        this._globalScale?.stepDown();
+    }
+
+    _screenBrightnessDownCurrentMonitor() {
+        const monitor = global.backend.get_current_logical_monitor();
+        this._monitorScales.get(monitor)?.stepDown();
+    }
+
+    _screenBrightnessCycle() {
+        this._globalScale?.cycleUp();
+    }
+
+    _screenBrightnessCycleCurrentMonitor() {
+        const monitor = global.backend.get_current_logical_monitor();
+        this._monitorScales.get(monitor)?.cycleUp();
+    }
+
     _monitorsChanged() {
         const monitors = global.backend
             .get_monitor_manager()
@@ -232,6 +305,21 @@ export const BrightnessScale = GObject.registerClass({
         this.notify('locked');
     }
 
+    stepUp() {
+        this._setValue(Math.min(1.0, this._brightness + SCALE_VALUE_STEP));
+    }
+
+    stepDown() {
+        this._setValue(Math.max(0.0, this._brightness - SCALE_VALUE_STEP));
+    }
+
+    cycleUp() {
+        if (Math.abs(1.0 - this._brightness) < SCALE_VALUE_CHANGE_EPSILON)
+            this._setValue(0.0);
+        else
+            this.stepUp();
+    }
+
     _setValue(value) {
         value = Math.clamp(value, 0.0, 1.0);
         if (Math.abs(value - this._value) < SCALE_VALUE_CHANGE_EPSILON)